/*
   Copyright 2011 
   
   DIFED

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
 */

package com.difed.cccam.db;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class DBHelper extends SQLiteOpenHelper {
    
    private static final String DATABASE_NAME = "cccam.db";
    private static final int DATABASE_VERSION = 1;
    
    public static final String TABLA_DATOS = "datos";
    public static final String TABLA_DATOS_ASOC = "datosAsociados";
    public static final String TABLA_CLIENTES = "clientes";
    public static final String TABLA_SERVIDORES = "servidores";
    public static final String TABLA_CLIENTE_SERVIDORES = "clientesServidores";
    public static final String TABLA_CONEXIONES = "conexiones";
    public static final String TABLA_CACHE = "cache";
    public static final String COL_ID = "id";
    public static final String COL_ID_CLIENTE = "idCliente";
    public static final String COL_ID_SERVIDOR = "idServidor";
    public static final String COL_ID_CONEXION = "idConexion";
    public static final String COL_ID_DATOS = "idDatos";
    public static final String COL_TITULO = "titulo";
    public static final String COL_CONTENIDO = "contenido";
    public static final String COL_TIPO = "tipo";
    public static final String COL_HOST = "host";
    public static final String COL_PUERTO = "puerto";
    public static final String COL_USUARIO = "usuario";
    public static final String COL_PASSWORD = "password";
    public static final String COL_ACTUALIZADO = "actualizado";
    public static final String COL_DATO_CACHE = "datoCache";
    
    public DBHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        // Tabla de Conexiones
        String sql = "CREATE TABLE IF NOT EXISTS " + TABLA_CONEXIONES + " ("
                    + COL_ID        + " INTEGER PRIMARY KEY AUTOINCREMENT, "
                    + COL_TITULO    + " TEXT NOT NULL, " 
                    + COL_HOST      + " TEXT NOT NULL, " 
                    + COL_PUERTO    + " TEXT, "
                    + COL_USUARIO   + " TEXT, "
                    + COL_PASSWORD  + " TEXT);";
        db.execSQL(sql);
        
        // Tabla de Clientes
        sql = "CREATE TABLE IF NOT EXISTS " + TABLA_CLIENTES + " ("
                    + COL_ID            + " INTEGER PRIMARY KEY AUTOINCREMENT, "
                    + COL_ID_CONEXION   + " INTEGER NOT NULL,"
                    + COL_USUARIO       + " TEXT NOT NULL);";
        db.execSQL(sql);
        
        // Tabla de Servidores
        sql = "CREATE TABLE IF NOT EXISTS " + TABLA_SERVIDORES + " ("
                    + COL_ID            + " INTEGER PRIMARY KEY AUTOINCREMENT, "
                    + COL_ID_CONEXION   + " INTEGER NOT NULL,"
                    + COL_HOST          + " TEXT NOT NULL);";
        db.execSQL(sql);
        
        // Tabla de Cliente - Servidores
        sql = "CREATE TABLE IF NOT EXISTS " + TABLA_CLIENTE_SERVIDORES + " ("
                    + COL_ID_CLIENTE    + " INTEGER NOT NULL, "
                    + COL_ID_CONEXION   + " INTEGER NOT NULL,"
                    + COL_ID_SERVIDOR   + " INTEGER NOT NULL);";
        db.execSQL(sql);
        
        // Tabla de Datos
        sql = "CREATE TABLE IF NOT EXISTS " + TABLA_DATOS + " ("
                    + COL_ID            + " INTEGER PRIMARY KEY AUTOINCREMENT, "
                    + COL_ID_CONEXION   + " INTEGER NOT NULL,"
                    + COL_ID_CLIENTE    + " INTEGER);";
        db.execSQL(sql);
        
        // Tabla de datos asociados
        sql = "CREATE TABLE IF NOT EXISTS " + TABLA_DATOS_ASOC +" ("
                    + COL_ID        + " INTEGER PRIMARY KEY AUTOINCREMENT, "
                    + COL_ID_DATOS  + " INTEGER NOT NULL, "
                    + COL_TIPO      + " TEXT NOT NULL, "
                    + COL_CONTENIDO + " TEXT NOT NULL);";
        db.execSQL(sql);
        
        sql = "CREATE TABLE IF NOT EXISTS " + TABLA_CACHE + " ("
            + COL_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
            + COL_ID_CONEXION   + " INTEGER NOT NULL,"
            + COL_TIPO + " INTEGER NOT NULL, "
            + COL_ACTUALIZADO + " INTEGER NOT NULL, "
            + COL_DATO_CACHE + " BLOB NOT NULL);";
        db.execSQL(sql);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        onCreate(db);
    }

}
